<template>
  <el-dialog :title="isEdit?'编辑作业点':'新增作业点'"
             :visible.sync="dialogForm"
             width="720px">
    <el-form :inline="true"
             ref="form"
             :model="form"
             :rules="rules"
             class="add-form"
             label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="作业点编号"
                        prop="resourceNo">
            <!-- <auto-search-line :name="form.resourceNo"></auto-search-line> -->
            <el-input v-model="form.resourceNo"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="作业点名称"
                        prop="resourceName">
            <!-- <auto-search-line :name="form.resourceName"></auto-search-line> -->
            <el-input v-model="form.resourceName"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属单位"
                        prop="orgId">
            <auto-search-org v-model="form.orgId"
                             @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                             @selectName="v=>form.orgName=v"
                             :disabled="true"
                             :name="form.orgName"></auto-search-org>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否有效"
                        prop="valid">
            <el-radio v-model="form.valid"
                      :label="true">是</el-radio>
            <el-radio v-model="form.valid"
                      :label="false">否</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注"
                        prop="resourceDescribe">
            <el-input type="textarea"
                      :rows="3"
                      style="width: 560px"
                      placeholder="请输入内容"
                      :maxLength="$maxLength"
                      v-model="form.resourceDescribe"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button @click="dialogForm = false">取 消</el-button>
      <el-button type="primary"
                 @click="handleSubmit"
                 :loading="$store.getters.loadingBtn">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  addOperationPoint,
  updateOperationPoint
} from '@/api/operationPoint/operationPoint'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import searchDept from '@/components/autocomplete/autoSearchDept'
import autoSearchLine from '@/components/autocomplete/autoSearchLine'
import autoSearchResource from '@/components/autocomplete/autoSearchResource'
import { maxLength, sortLength } from '@/utils/rules'
import { PUBLIC_LIMIT, PUBLIC_NUM_CODE } from '@/utils/Reg'

export default {
  name: 'operationPointAddForm',
  components: { autoSearchOrg, searchDept, autoSearchLine, autoSearchResource },
  data() {
    return {
      form: this.clearForm(),
      rules: {
        resourceNo: [
          { required: true, validator: PUBLIC_NUM_CODE, trigger: 'blur' },
          sortLength
        ],
        resourceName: [
          { required: true, validator: PUBLIC_LIMIT, trigger: 'blur' },
          sortLength
        ],
        orgId: [{ validator: PUBLIC_LIMIT, trigger: 'change' }],
        valid: [{ required: true, message: '请选择是否有效', trigger: 'blur' }],
        resourceDescribe: [maxLength]
      },
      dialogForm: false,
      isEdit: false,
      maxLot: 0,
      checkList: [],
      radio: true
    }
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name,
        resourceNo: '',
        resourceName: '',
        valid: true,
        resourceDescribe: ''
      }
    },
    // 保存
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.handleToEdit() : this.handleToAdd()
        } else {
          this.$message({
            message: '请检查表单',
            type: 'error'
          })
        }
      })
    },
    // 新增
    handleToAdd() {
      addOperationPoint(this.form).then(res => {
        this.$message({ message: '新增成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    // 编辑
    handleToEdit() {
      updateOperationPoint(this.form.id, this.form).then(res => {
        this.$message({ message: '编辑成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    }
    // getLineInfo(item){
    //     if(item){
    //         this.form.lineNo=item.resourceNo
    //         this.form.lineId=item.id
    //         this.maxLot=item.maxParkingNum
    //     }else{
    //         this.form.lineNo=''
    //         this.form.lineId=''
    //         this.maxLot=0
    //         this.form=this.clearForm()
    //     }
    // }
  },
  watch: {
    dialogForm(bool) {
      if (!bool) {
        this.$refs.form.resetFields()
        this.isEdit = false
        this.form = this.clearForm()
        this.$emit('clear')
      }
    }
  }
}
</script>

<style scoped>
</style>
